package views;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import common.Contexto;

import controllers.IErrorObserver;
import controllers.ReservasController;
import data.entities.ReservaCancha;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

/**
 * Pantalla que permite cargar los datos de una nueva reserva de cancha
 * 
 */
@SuppressWarnings("serial")
public class ReservaCanchaPanel extends JPanel  implements IErrorObserver {
	private JLabel nroDocL, diaL, horarioL;

	private JTextField nroDocT;
	
	private JDatePickerImpl datePicker;
	private UtilDateModel dateModel;

	private JComboBox horarioC;
	
	private JButton buscarEspacioB, volverB;
	
	private String nroDoc;

	private ReservasController controller;

	public ReservaCanchaPanel() {
		controller = new ReservasController();
		this.controller.agregarErrorObserver(this);
		initGui();
		layoutComponents();
		initActions();
	}

	private void initGui() {
		nroDocL = new JLabel("* Nro doc.");
		diaL = new JLabel("* Dia");
		horarioL = new JLabel("* Horario");
		
		Integer[] horariosValues = new Integer[24];
		for(int i = 1; i < horariosValues.length; i++){
			Integer horarioValue = new Integer(i);
			horariosValues[i-1] = horarioValue;
		}
		horarioC = new JComboBox(horariosValues);
		horarioC.setSelectedIndex(18);
			
		buscarEspacioB = new JButton();
		volverB = new JButton();

		nroDocT = new JTextField(10);
		dateModel = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(dateModel);
		datePicker = new JDatePickerImpl(datePanel);
	}

	private void layoutComponents() {
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST ;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0, 2, 2, 4);
        add(nroDocL, c);

        c.gridy = GridBagConstraints.RELATIVE;
        add(diaL, c);
        add(horarioL, c);
        
        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST ;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0, 2, 2, 4);
        add(nroDocT, c);

        c.gridy = GridBagConstraints.RELATIVE;
        add(datePicker, c);
        add(horarioC, c);
        
        c.gridx = 0;
        c.gridy = 3;
        c.insets = new Insets(20, 0, 10, 0);
        
        
    	add(buscarEspacioB, c);
        
        c.gridx = 1;
        add(volverB, c);

	}
	
	private void initActions() {
		buscarEspacioB.setAction(new AbstractAction("Buscar mejor espacio"){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(validateFields()) {
					Date fecha = (Date) datePicker.getModel().getValue();
					controller.buscarMejorEspacio(nroDoc, fecha, (Integer)horarioC.getSelectedItem());
					disposePanel();
					clearFields();
				}
			}			
		});
		
		volverB.setAction(new AbstractAction("Cancelar"){
			@Override
			public void actionPerformed(ActionEvent e) {
				disposePanel();
				clearFields();
			}			
		});
	}
	
	private boolean validateFields() {
		if(nroDocT.getText().length() > 0){
			nroDoc = nroDocT.getText();
			return true;
		}
		
		return false;
	}
	
	private void clearFields() {
		nroDocT.setText("");
		datePicker.getModel().setValue(null);
		horarioC.setSelectedIndex(18);
	}
	
	private void disposePanel() {
		SwingUtilities.windowForComponent(ReservaCanchaPanel.this).dispose();
	}
	
	@Override
	public void notificarError(String error) {
		Contexto.displayErrorMessage(this, error);
	}
	
	public void showPanel() {
		final JFrame mainFrame = new JFrame();
        mainFrame.setTitle("Reserva de Cancha");

        mainFrame.getContentPane().add(this);
        mainFrame.setPreferredSize(new Dimension(700,340));
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
	}
	
	public static void main(String[] args) {
		ReservaCanchaPanel clasePanel = new ReservaCanchaPanel();
		clasePanel.showPanel();
	}

	
}
